package com.cfets.annualAssessment.weixinService.bean;

import com.cfets.annualAssessment.util.Constant;
import com.cfets.annualAssessment.util.RedisRunner;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.util.StringUtils;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;

import javax.net.ssl.SSLContext;
import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: pual
 * Date: 2015/12/4
 * Desc:
 */
public class WxMpInRedisConfigStorage implements WxMpConfigStorage {

    protected volatile String appId;
    protected volatile String secret;
    protected volatile String partnerId;
    protected volatile String partnerKey;
    protected volatile String token;
    protected volatile String accessToken;
    protected volatile String aesKey;
//    protected volatile long expiresTime;

    protected volatile String oauth2redirectUri;

    protected volatile String http_proxy_host;
    protected volatile int http_proxy_port;
    protected volatile String http_proxy_username;
    protected volatile String http_proxy_password;

    protected volatile String jsapiTicket;
    protected volatile long jsapiTicketExpiresTime;
    private RedisRunner redisRunner;

    /**
     * 临时文件目录
     */
    protected volatile File tmpDirFile;

    public String getAccessToken() {
        return redisRunner.cacheGet(Constant.REDIS_WX_DB, Constant.REDIS_WX_ACCESSTOKEN);
    }

    public boolean isAccessTokenExpired() {
        String token = redisRunner.cacheGet(Constant.REDIS_WX_DB,Constant.REDIS_WX_ACCESSTOKEN);
        return StringUtils.isBlank(token);
    }

    public synchronized void updateAccessToken(WxAccessToken accessToken) {
        updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
    }

    public synchronized void updateAccessToken(String accessToken, int expiresInSeconds) {
        redisRunner.cacheAddUpdate(Constant.REDIS_WX_DB,Constant.REDIS_WX_ACCESSTOKEN,accessToken,Constant.REDIS_WX_ACCESSTOKEN_EXPIRY);
    }

    public void expireAccessToken() {
        redisRunner.cacheExpiry(Constant.REDIS_WX_DB,Constant.REDIS_WX_ACCESSTOKEN,0);
    }

    public String getJsapiTicket() {
        return redisRunner.cacheGet(Constant.REDIS_WX_DB, Constant.REDIS_WX_TICKET);
    }

    public void setJsapiTicket(String jsapiTicket) {
        this.jsapiTicket = jsapiTicket;
    }

    public long getJsapiTicketExpiresTime() {
        return jsapiTicketExpiresTime;
    }

    public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
        this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
    }

    public boolean isJsapiTicketExpired() {
//        return System.currentTimeMillis() > this.jsapiTicketExpiresTime;
        String token = redisRunner.cacheGet(Constant.REDIS_WX_DB,Constant.REDIS_WX_TICKET);
        return StringUtils.isBlank(token);
    }

    public synchronized void updateJsapiTicket(String jsapiTicket, int expiresInSeconds) {
//        this.jsapiTicket = jsapiTicket;
//        // 预留200秒的时间
//        this.jsapiTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000l;
        redisRunner.cacheAddUpdate(Constant.REDIS_WX_DB,Constant.REDIS_WX_TICKET,jsapiTicket,Constant.REDIS_WX_TICKET_EXPIRY);
    }

    public void expireJsapiTicket() {
//        this.jsapiTicketExpiresTime = 0;
        redisRunner.cacheExpiry(Constant.REDIS_WX_DB,Constant.REDIS_WX_TICKET,0);
    }

    public String getAppId() {
        return redisRunner.cacheGet(Constant.REDIS_WX_DB,Constant.REDIS_WX_APPID);
    }

    public String getSecret() {
        return redisRunner.cacheGet(Constant.REDIS_WX_DB,Constant.REDIS_WX_APPSECRET);
    }

    public String getPartnerId() {
        return null;
    }

    public String getPartnerKey() {
        return null;
    }

    public String getToken() {
        return redisRunner.cacheGet(Constant.REDIS_WX_DB,Constant.REDIS_WX_TOKEN);
    }

    public String getAesKey() {
        return redisRunner.cacheGet(Constant.REDIS_WX_DB,Constant.REDIS_WX_AESKEY);
    }

    public long getExpiresTime() {
        return 0;
    }

    public String getOauth2redirectUri() {
        return null;
    }

    public void setOauth2redirectUri(String oauth2redirectUri) {
        this.oauth2redirectUri = oauth2redirectUri;
    }

    public String getHttp_proxy_host() {
        return http_proxy_host;
    }

    public void setHttp_proxy_host(String http_proxy_host) {
        this.http_proxy_host = http_proxy_host;
    }

    public int getHttp_proxy_port() {
        return http_proxy_port;
    }

    public void setHttp_proxy_port(int http_proxy_port) {
        this.http_proxy_port = http_proxy_port;
    }

    public String getHttp_proxy_username() {
        return http_proxy_username;
    }

    public void setHttp_proxy_username(String http_proxy_username) {
        this.http_proxy_username = http_proxy_username;
    }

    public String getHttp_proxy_password() {
        return http_proxy_password;
    }

    public File getTmpDirFile() {
        return null;
    }

    public void setHttp_proxy_password(String http_proxy_password) {
        this.http_proxy_password = http_proxy_password;
    }

    @Override
    public String toString() {
        return "WxMpInMemoryConfigStorage{" +
                "appId='" + appId + '\'' +
                ", secret='" + secret + '\'' +
                ", token='" + token + '\'' +
                ", partnerId='" + partnerId + '\'' +
                ", partnerKey='" + partnerKey + '\'' +
                ", accessToken='" + accessToken + '\'' +
                ", aesKey='" + aesKey + '\'' +
                ", http_proxy_host='" + http_proxy_host + '\'' +
                ", http_proxy_port=" + http_proxy_port +
                ", http_proxy_username='" + http_proxy_username + '\'' +
                ", http_proxy_password='" + http_proxy_password + '\'' +
                ", jsapiTicket='" + jsapiTicket + '\'' +
                ", jsapiTicketExpiresTime='" + jsapiTicketExpiresTime + '\'' +
                ", tmpDirFile='" + tmpDirFile + '\'' +
                '}';
    }

    public void setPartnerId(String partnerId) {
        this.partnerId = partnerId;
    }

    public void setPartnerKey(String partnerKey) {
        this.partnerKey = partnerKey;
    }

    public SSLContext getSSLContext() {
        return null;
    }

    public void setTmpDirFile(File tmpDirFile) {
        this.tmpDirFile = tmpDirFile;
    }

    public RedisRunner getRedisRunner() {
        return redisRunner;
    }

    public void setRedisRunner(RedisRunner redisRunner) {
        this.redisRunner = redisRunner;
    }
}
